package com.ibm.cps.servlet;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.ibm.util.LocalConfig;
import com.ibm.util.exception.CPSException;

public class HttpClientMonitor {

	private static final Logger LOGGER = Logger
			.getLogger(HttpClientMonitor.class);

	// static String CPSRESTFUL_SERVER = "http://localhost:8080/cpsrestful1/";
	// static String CPSRESTFUL_SERVER = "http://9.186.89.220:8080/cpsrestful";
	// static String CPSRESTFUL_SERVER = "http://9.186.88.253:8080/cpsrestful1";
	static CloseableHttpClient httpClient = HttpClients.createDefault();

	static {
		try {
			PropertyConfigurator.configure(LocalConfig.getLoggerProperties());
		} catch (CPSException e) {
			e.printStackTrace();
		}
	}

	public static String sendRequestWithBody(String url, String content,
			HTTP_REQUEST_METHOD method) throws ClientProtocolException,
			IOException {
		LOGGER.info("URL: " + url);
		LOGGER.info(method.name() + ": " + content);

		HttpEntityEnclosingRequestBase request = getRequest(url, method);

		StringEntity stringEntity = new StringEntity(content, "UTF-8");
		request.setEntity(stringEntity);
		CloseableHttpResponse httpResponse = httpClient.execute(request);
		if (httpResponse.getEntity() != null) {
			HttpEntity httpEntity = httpResponse.getEntity();
			String response = EntityUtils.toString(httpEntity);
			if (httpResponse.getStatusLine().getStatusCode() != 200) {
				LOGGER.error(response);
			} else {
				LOGGER.info(response);
			}
			httpResponse.close();
			return response;
		} else {
			httpResponse.close();
			return "No response from entity.";
		}
	}

	public static String sendGetRequest(String url)
			throws ClientProtocolException, IOException {
		LOGGER.info("URL get: " + url);

		HttpGet httpGet = new HttpGet(url);
		httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
		CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
		HttpEntity httpEntity = httpResponse.getEntity();
		String response = EntityUtils.toString(httpEntity);
		if (httpResponse.getStatusLine().getStatusCode() != 200) {
			LOGGER.error(response);
		} else {
			LOGGER.info(response);
		}
		httpResponse.close();
		return response;
	}

	public static String addURLParameter(String key, String value,
			String initURL) {

		// first parameter
		if (initURL.endsWith("?")) {
			return initURL += key + "=" + value;
		} else {
			return initURL += "&" + key + "=" + value;
		}
	}

	public static HttpEntityEnclosingRequestBase getRequest(String url,
			HTTP_REQUEST_METHOD method) {
		if (method == null) {
			return null;
		}

		if (method.equals(HTTP_REQUEST_METHOD.post)) {
			return new HttpPost(url);
		}

		if (method.equals(HTTP_REQUEST_METHOD.put)) {
			return new HttpPut(url);
		}
		if (method.equals(HTTP_REQUEST_METHOD.delete)) {
			return new HttpDeleteWithBody(url);
		}
		if (method.equals(HTTP_REQUEST_METHOD.patch)) {
			return new HttpPatch(url);
		}
		return null;
	}

	public enum HTTP_REQUEST_METHOD {
		get, post, delete, put, patch
	}
}
